package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.Tag;
import cn.wolfcode.trip.base.service.ITagService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 标签控制器
 */
@RestController
@RequestMapping("tags")
public class TagController {
    @Autowired
    private ITagService tagService;

    /**
     * 查询所有的标签
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public List<Tag> query(){
        return  tagService.query();
    }

    /**
     * 保存标签
     */
    @RequestMapping(value = "/save/{name}/{strategyId}",method = RequestMethod.POST)
    public JSONResult save(Tag tag,@PathVariable Long strategyId){
        JSONResult result = new JSONResult();
        try {
            Strategy strategy = new Strategy();
            strategy.setId(strategyId);
            tagService.save(tag,strategy);
            result.setResult("1");
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
    @RequestMapping(value = "/saveRelation/{id}/{strategyId}",method = RequestMethod.POST)
    public JSONResult saveRelation(@PathVariable Long id,@PathVariable Long strategyId){
        JSONResult result = new JSONResult();
        try {
            tagService.saveRelation(id,strategyId);
            result.setResult("1");
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
}
